# Replication
# John Gerring, Carl Henrik Knutsen, Matthew Maguire, Svend-Erik Skaaning, Jan Teorell, and Michael Coppedge. 2021. “Democracy and Human Development: Issues of Conceptualization and Measurement.” Democratization 28(2): 308-332.

# Clear list objects
rm(list=ls())

# load packages
library(dotwhisker)
library(broom)
library(dplyr)
library(foreign)
library(ggplot2)
library(plm)
library(lmtest)

# Set directory

# Load data
hd_data <- read.dta("2021_Democratization_replication_data_10yr.dta")

# Models

# 1. Multiplication (MPI)
mod.1 <- plm(imr_gapminder_ipo_ln ~ lag(edi_mult_stock_10_std) + lag(e_migdppcln_ipo), data = hd_data, index = c("country_id","trend"), method = "within", effect = "twoways")
G <- length(unique(hd_data$country_id))
c <- G/(G - 1)
mod.1.cse <- coeftest(mod.1,c * vcovHC(mod.1, type = "HC1", cluster = "group"))
term<-c("Multiplication (MPI)")
estimate<-c(mod.1.cse[1,1])
std.error<-c(mod.1.cse[1,2])
model <- c("Multiplication (MPI)")
m1<-data.frame(term,estimate,std.error,model)

# 2. Minimum (weakest link)
mod.1 <- plm(imr_gapminder_ipo_ln ~ lag(edi_min_stock_10_std) + lag(e_migdppcln_ipo), data = hd_data, index = c("country_id","trend"), method = "within", effect = "twoways")
G <- length(unique(hd_data$country_id))
c <- G/(G - 1)
mod.1.cse <- coeftest(mod.1,c * vcovHC(mod.1, type = "HC1", cluster = "group"))
term<-c("Minimum (weakest link)")
estimate<-c(mod.1.cse[1,1])
std.error<-c(mod.1.cse[1,2])
model <- c("Minimum (weakest link)")
m2<-data.frame(term,estimate,std.error,model)

# 3. Electoral Democracy Index (addition and multiplication)
mod.1 <- plm(imr_gapminder_ipo_ln ~ lag(v2x_polyarchy_stock_10_std) + lag(e_migdppcln_ipo), data = hd_data, index = c("country_id","trend"), method = "within", effect = "twoways")
G <- length(unique(hd_data$country_id))
c <- G/(G - 1)
mod.1.cse <- coeftest(mod.1,c * vcovHC(mod.1, type = "HC1", cluster = "group"))
term<-c("Electoral Democracy Index (addition and multiplication)")
estimate<-c(mod.1.cse[1,1])
std.error<-c(mod.1.cse[1,2])
model <- c("Electoral Democracy Index (addition and multiplication)")
m3<-data.frame(term,estimate,std.error,model)

# 4. Factor scores (principal components)
mod.1 <- plm(imr_gapminder_ipo_ln ~ lag(edi_pca_stock_10_std) + lag(e_migdppcln_ipo), data = hd_data, index = c("country_id","trend"), method = "within", effect = "twoways")
G <- length(unique(hd_data$country_id))
c <- G/(G - 1)
mod.1.cse <- coeftest(mod.1,c * vcovHC(mod.1, type = "HC1", cluster = "group"))
term<-c("Factor scores (principal components)")
estimate<-c(mod.1.cse[1,1])
std.error<-c(mod.1.cse[1,2])
model <- c("Factor scores (principal components)")
m4<-data.frame(term,estimate,std.error,model)

# 5. Average (arithmetic)
mod.1 <- plm(imr_gapminder_ipo_ln ~ lag(edi_ave_stock_10_std) + lag(e_migdppcln_ipo), data = hd_data, index = c("country_id","trend"), method = "within", effect = "twoways")
G <- length(unique(hd_data$country_id))
c <- G/(G - 1)
mod.1.cse <- coeftest(mod.1,c * vcovHC(mod.1, type = "HC1", cluster = "group"))
term<-c("Average (arithmetic)")
estimate<-c(mod.1.cse[1,1])
std.error<-c(mod.1.cse[1,2])
model <- c("Average (arithmetic)")
m5<-data.frame(term,estimate,std.error,model)

# plot
dwplot(rbind(m1,m2,m3,m4,m5)) + 
  theme_bw() +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  xlab("Coefficient Estimate") +
  scale_color_grey(start = .1, end = .1) +
  theme(axis.title.x=element_text(size=rel(0.9))) +
  theme(legend.position = "none", legend.title = element_blank())
ggsave("Figure_2.pdf")